<template>
	<view class="notice">
		<g-navbar title="系统公告" />
		<view class="main">
			<scroll-view scroll-y class="scroll-view">
				<view class="notice-list-wrapper">
					<view class="notice-item" v-for="item in noticeList" :key="item.id">
						<view class="publish-date">
							{{item.created_at}}
						</view>
						<view class="notice-item-main">
							<view class="notice-title">
								{{item.title?item.title:item.content}}
							</view>
							<view class="todetail-btn" @click="jump(item.id)">
								查看详情
							</view>
						</view>
					</view>
					<g-placeholder v-if="!noticeList.length&&pagination.loadingStatus===LOADING_STATUS.NOMORE" />
					<uni-load-more :status="pagination.loadingStatus" v-else />
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { IPagination, IGetNoticeListOptions, IGetNoticeListResult } from './types';
	import { reactive } from "vue";
	import { LOADING_STATUS } from '@/enum/loadingStatus';
	import { INotice } from '@/types/notice';
	import { useStore } from 'vuex';
	import REST_URL from '@/http/http.restUrl';
	import { onLoad } from '@dcloudio/uni-app';
	import { removeHTMLTag, escape2Html } from '@/utils';

	onLoad(async () => {
		// await uni.loginManger.$interceptor;
		await getNoticeList();
	})

	const store = useStore();

	const pagination : IPagination = reactive({
		pageNum: 1,
		pageSize: 20,
		loadingStatus: LOADING_STATUS.MORE
	})

	const noticeList : INotice[] = reactive([]);


	const getNoticeList = async () : Promise<void> => {
		if (pagination.loadingStatus !== LOADING_STATUS.MORE) return
		const options : IGetNoticeListOptions = {
			// customer_id: store.state.userInfo.id,
			page: pagination.pageNum,
			page_num: pagination.pageSize,
		}

		pagination.loadingStatus = LOADING_STATUS.LOADING;
		const { data, total } : IGetNoticeListResult = (await uni.http.POST(REST_URL.systemNoticeList, options)).data;
		for (let item of data) {
			item.content = contentFormat(item.content);
			noticeList.push(item);
		}
		pagination.loadingStatus = noticeList.length >= total ? LOADING_STATUS.NOMORE : LOADING_STATUS.MORE;
		pagination.pageNum++;
	}

	const contentFormat = (content : string) : string => {
		const str : string = escape2Html(removeHTMLTag(content)).trim();
		const temp : string[] = [];
		for (let item of str) {
			if (item.trim()) {
				temp.push(item);
			}
		}
		return temp.join('');
	}

	const jump = (id : number) => {
		uni.router.route({
			url: '/pages_notice/notice_detail/noticeDetail',
			params: {
				id,
			}
		})
	}
</script>

<style scoped lang="scss">
	@import "./notice.scss";
</style>